home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / nuweb / msdos / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-01  |  5.6 KB  |  165 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #ifndef FALSE
  6. #define FALSE 0
  7. #endif
  8. #ifndef TRUE
  9. #define TRUE (!0)
  10. #endif
  11. typedef struct scrap_node {
  12.   struct scrap_node *next;
  13.   int scrap;
  14. } Scrap_Node;
  15. typedef struct name {
  16.   char *spelling;
  17.   struct name *llink;
  18.   struct name *rlink;
  19.   Scrap_Node *defs;
  20.   Scrap_Node *uses;
  21.   int mark;
  22.   char tab_flag;
  23.   char indent_flag;
  24.   char debug_flag;
  25. } Name;
  26.  
  27. int tex_flag;      /* if FALSE, don't emit the .tex file */
  28. int output_flag;   /* if FALSE, don't emit the output files */
  29. int compare_flag;  /* if FALSE, overwrite without comparison */
  30. char *command_name;
  31. char *source_name;  /* name of the current file */
  32. int source_line;    /* current line in the source file */
  33. Name *file_names;
  34. Name *macro_names;
  35. Name *user_names;
  36.  
  37. void pass1();
  38. void write_tex();
  39. void write_files();
  40. void source_open(); /* pass in the name of the source file */
  41. int source_get();   /* no args; returns the next char or EOF */
  42. void init_scraps();
  43. int collect_scrap();
  44. int write_scraps();
  45. Name *collect_file_name();
  46. Name *collect_macro_name();
  47. Name *collect_scrap_name();
  48. Name *name_add();
  49. Name *prefix_add();
  50. char *save_string();
  51. void reverse_lists();
  52. void *arena_getmem();
  53. void arena_free();
  54.  
  55.  
  56. static FILE *source_file;  /* the current input file */
  57. static int source_peek;
  58. static int double_at;
  59. static int include_depth;
  60. struct {
  61.   FILE *file;
  62.   char *name;
  63.   int line;
  64. } stack[10];
  65. int source_get()
  66. {
  67.   int c = source_peek;
  68.   switch (c) {
  69.     case EOF:  {
  70.                  fclose(source_file);
  71.                  if (include_depth) {
  72.                    include_depth--;
  73.                    source_file = stack[include_depth].file;
  74.                    source_line = stack[include_depth].line;
  75.                    source_name = stack[include_depth].name;
  76.                    source_peek = getc(source_file);
  77.                    c = source_get();
  78.                  }
  79.                }
  80.                return c;
  81.     case '@':  {
  82.                  c = getc(source_file);
  83.                  if (double_at) {
  84.                    source_peek = c;
  85.                    double_at = FALSE;
  86.                    c = '@';
  87.                  }
  88.                  else
  89.                    switch (c) {
  90.                      case 'i': {
  91.                                  if (include_depth < 10) {
  92.                                    char name[100];
  93.                                    {
  94.                                        char *p = name;
  95.                                        do 
  96.                                          c = getc(source_file);
  97.                                        while (c == ' ' || c == '\t');
  98.                                        while (isgraph(c)) {
  99.                                          *p++ = c;
  100.                                          c = getc(source_file);
  101.                                        }
  102.                                        *p = '\0';
  103.                                        if (c != '\n') {
  104.                                          fprintf(stderr, "%s: unexpected characters after file name (%s, %d)\n",
  105.                                                  command_name, source_name, source_line);
  106.                                          exit(-1);
  107.                                        }
  108.                                    }
  109.                                    stack[include_depth].name = source_name;
  110.                                    stack[include_depth].file = source_file;
  111.                                    stack[include_depth].line = source_line + 1;
  112.                                    include_depth++;
  113.                                    source_line = 1;
  114.                                    source_name = save_string(name);
  115.                                    source_file = fopen(source_name, "r");
  116.                                    if (!source_file) {
  117.                                      fprintf(stderr, "%s: can't open include file %s\n",
  118.                                              command_name, source_name);
  119.                                      exit(-1);
  120.                                    }
  121.                                    source_peek = getc(source_file);
  122.                                    c = source_get();
  123.                                  }
  124.                                  else {
  125.                                    fprintf(stderr, "%s: include nesting too deep (%s, %d)\n",
  126.                                            command_name, source_name, source_line);
  127.                                    exit(-1);
  128.                                  }
  129.                                }
  130.                                break;
  131.                      case 'f': case 'm': case 'u':
  132.                      case 'd': case 'o': case 'D': case 'O':
  133.                      case '{': case '}': case '<': case '>': case '|':
  134.                                source_peek = c;
  135.                                c = '@';
  136.                                break;
  137.                      case '@': source_peek = c;
  138.                                double_at = TRUE;
  139.                                break;
  140.                      default:  fprintf(stderr, "%s: bad @ sequence (%s, line %d)\n",
  141.                                        command_name, source_name, source_line);
  142.                                exit(-1);
  143.                    }
  144.                }
  145.                return c;
  146.     case '\n': source_line++;
  147.     default:   source_peek = getc(source_file);
  148.                return c;
  149.   }
  150. }
  151. void source_open(name)
  152.      char *name;
  153. {
  154.   source_file = fopen(name, "r");
  155.   if (!source_file) {
  156.     fprintf(stderr, "%s: couldn't open %s\n", command_name, name);
  157.     exit(-1);
  158.   }
  159.   source_name = name;
  160.   source_line = 1;
  161.   source_peek = getc(source_file);
  162.   double_at = FALSE;
  163.   include_depth = 0;
  164. }
  165.